import { observable, computed, action, configure, flow } from 'mobx';
import _ from 'lodash';
import { Actions } from 'react-native-router-flux';
import { Alert } from 'react-native';
import toast from '../../../components/toast';
import members from './members';
import policies from './policies';
import customers from './customers';
import { getCustomer, updateCustomer, createCustomer } from '../services';

configure({ enforceActions: true });
const alert = Alert.alert;

class Customer {
  @observable customer = {};
  @observable pending = false;
  @observable submitStatus = false;
  @computed
  get id() {
    return this.customer.customerId;
  }
  set id(newId) {
    this.customer.customerId = newId;
  }

  @action
  resetCurrent = () => {
    this.customer = {};
  };
  @action
  submit = stop => {
    if (stop) {
      this.submitStatus = false;
      return;
    }
    this.submitStatus = true;
  };
  init = flow(function* (id) {
    this.pending = true;
    try {
      yield [
        this.getCustomer(id),
        members.fetch(id, true),
        policies.fetch(id, true)
      ];
    } finally {
      this.pending = false;
    }
  });
  getCustomer = flow(function* (id, force) {
    if (force || _.isEmpty(this.customer) || this.customer.customerId !== id) {
      this.pending = true;
      try {
        const data = yield getCustomer(id);
        if (data) {
          this.customer = data;
          this.id = id;
        }
      } finally {
        this.pending = false;
      }
    }
  });
  create = flow(function* (value) {
    this.pending = true;
    try {
      delete value.customerId;
      const data = yield createCustomer(value);
      if (data) {
        this.submitStatus = false;
        this.pending = false;
        this.updateList(data.customerId, () => {
          Actions.replace('customerBaseCompleted', { data: { ...data } });
        });
      }
    } catch(e){} finally {
      this.pending = false;
      this.submitStatus = false;
    }
  });
  updateList = flow(function* (id, finallyCallBack) {
    try {
      const allCustomers = yield storage.getAllDataForKey('customers');
      const newCustomer = yield getCustomer(id);
      if (allCustomers.length > 0 && newCustomer) {
        storage.save({
          key: 'customers',
          id,
          data: newCustomer
        });
        if (!finallyCallBack) {
          this.customer = newCustomer;
          this.id = id;
        }
      }
    } finally {
      customers.setReloadStatus.call(customers, true);
      _.isFunction(finallyCallBack) && finallyCallBack();
    }
  });
  update = flow(function* (value) {
    this.pending = true;
    try {
      const _v = { ...this.customer, ...value };
      _.forEach(_v, (v, k) => {
        if (/Name$/.test(k)) {
          delete _v[k];
        }
      });
      const data = yield updateCustomer(_v);
      if (data) {
        this.pending = false;
        this.submitStatus = false;
        yield this.updateList(_v.customerId);
        return _v.name;
      }
    } finally {
      this.pending = false;
      this.submitStatus = false;
    }
  });
  @action
  resetCustomer = () => {
    this.customer = {};
  };
}

export default new Customer();
